package com.topscomm.meet.controller;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.meet.controller.auto.FileManageControllerAuto;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SecurityUtil;
import com.topscomm.tap.common.ParameterCache;

import cn.hutool.core.io.FileUtil;

@RestController
@RequestMapping("play")
public class AudioPlayController extends FileManageControllerAuto implements IFileManageController {
	/**
	 * 音视频存储路径
	 */
	@Value("${file.audio.root.path}")
	private String blockFolder;
	/**
	 * 音视频显示路径
	 */
	@Value("${file.audio.root.url}")
	private String displayUrl;
	/**
	 * 音视频本地路径
	 */
	@Value("${file.audio.root.localurl}")
	private String displayLocalUrl;
	private String rootUrlTag = "\\$\\{url\\}";

	/**
	 * 得到视频服务器url
	 *
	 * @description:
	 * @param paraMap
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @author: donghang
	 * @date: 2020年9月14日下午6:51:24
	 * @modify:
	 */
	@PostMapping(value = "/getBlobSrc")
	public void getBlobRemoteSrc(@RequestBody Map<String, Object> paraMap, HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) {
		this.getBlob(paraMap, httpServletRequest, httpServletResponse, this.displayUrl);
	}

	/**
	 * 得到本地服务url
	 *
	 * @description:
	 * @param paraMap
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @author: donghang
	 * @date: 2020年9月14日下午6:53:48
	 * @modify:
	 */

	@PostMapping(value = "/getBlobLocalSrc")
	public void getBlobLocalSrc(@RequestBody Map<String, Object> paraMap, HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) {
		this.getBlob(paraMap, httpServletRequest, httpServletResponse, this.displayLocalUrl);
	}

	/**
	 *
	 * @description:
	 * @param paraMap
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @param url
	 * @author: donghang
	 * @date: 2020年9月14日下午6:58:25
	 * @modify:
	 */
	public void getBlob(Map<String, Object> paraMap, HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse, String url) {
		String location = ConvertUtil.convertToString(paraMap.get("qry_location"));
		String m3u8FileString = String.format("%s/%s", blockFolder, location);
			byte[] secretContent = FileUtil.readBytes(m3u8FileString);
			String secretKey = ParameterCache.getValue("m3u8SecretKey", "12345678901234567890123456789012");
			byte[] trueContent = SecurityUtil.decryptDes(secretContent, secretKey);
			String contentString = new String(trueContent);
			// 远程key url转成本地的url
			contentString = contentString.replaceAll(displayUrl, url);
			// ts分片url转成本地的
			contentString = contentString.replaceAll(rootUrlTag, url);

			httpServletResponse.reset();
			// 6.设置response编码
			httpServletResponse.setCharacterEncoding("UTF-8");
			httpServletResponse.addHeader("Content-Length", "" + contentString.getBytes().length);
			// 设置输出文件类型
			httpServletResponse.setContentType("application/x-mpegURL");
			httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
            try(// 7.获取response输出流
                OutputStream outputStream = httpServletResponse.getOutputStream();
                // byte[] trueContent = secretContent;
                InputStream inputStream = new ByteArrayInputStream(contentString.getBytes());){

                int byteRead;
                while ((byteRead = inputStream.read()) != -1) {
                    outputStream.write(byteRead);
                }
            }catch (IOException e){
                e.printStackTrace();
            }
	}
}
